//
//  ZLAlert+ZLTextField.m
//  ZLAlert-OC
//
//  Created by 科大 on 2024/1/2.
//

#import "ZLAlert+ZLTextField.h"

@implementation ZLAlertTextField
+ (instancetype)zl_textFieldWithText:(NSString *)text
                         placeholder:(NSString *)placeholder
                              config:(ZLAlertItemConfig *)config {
    ZLAlertTextField *textField = [[ZLAlertTextField alloc] init];
    textField.text = text;
    textField.font = config.font;
    textField.textColor = config.color;
    textField.placeholder = placeholder;
    textField.textAlignment = config.alignment;
    textField.backgroundColor = config.backgroundColor;
    textField.layer.cornerRadius = config.cornerRadius;
    textField.layer.borderWidth = config.borderWidth;
    textField.layer.borderColor = config.borderColor.CGColor;
    return textField;
}
@end

@implementation ZLAlert (ZLTextField)

- (ZLAlert *)addTextField:(NSString *)placeholder editEnd:(nullable ZLAlertEditEnd)editEnd {
    return [self addTextField:@"" placeholder:placeholder config:ZLAlertConfig.shared.inputConfig editChange:nil editEnd:editEnd];
}

- (ZLAlert *)addTextField:(NSString *)text
              placeholder:(NSString *)placeholder
                   config:(ZLAlertItemConfig *)config
               editChange:(nullable ZLAlertEditChange)editChange
                  editEnd:(nullable ZLAlertEditEnd)editEnd {
    UIView *lastView = self.mainView.subviews.lastObject;
    ZLAlertTextField *textField = [ZLAlertTextField zl_textFieldWithText:text placeholder:placeholder config:config];
    textField.editChange = editChange;
    textField.editEnd = editEnd;
    textField.delegate = self;
    [self.mainView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mainView).offset(config.insets.left);
        make.right.equalTo(self.mainView).offset(-config.insets.right);
        if (lastView == nil) {
            make.top.equalTo(self.mainView).offset(config.insets.top);
        } else {
            make.top.equalTo(lastView.mas_bottom).offset(config.insets.top);
        }
        make.height.mas_equalTo(config.height);
    }];
    return [self layoutMainView:0];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    if (self.mainView.textField == nil) {
        return;
    }
    
    CGRect rect1 = [self.mainView.textField convertRect:self.mainView.textField.frame fromView:self.mainView];
    CGRect rect = [self.mainView.textField convertRect:rect1 toView:self.view];
    
    NSDictionary *userInfo = notification.userInfo;
    NSValue *value = userInfo[UIKeyboardFrameEndUserInfoKey];
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSUInteger curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
    
    CGRect frame = [value CGRectValue];
    CGRect intersection = CGRectIntersection(frame, self.view.frame);
    CGFloat deltaY = CGRectGetHeight(intersection) - (self.view.bounds.size.height - CGRectGetMaxY(rect));
    
    if (deltaY < 0) {
        return;
    }
    
    [UIView animateWithDuration:duration delay:0.0 options:(UIViewAnimationOptions)curve animations:^{
        self.view.frame = CGRectMake(0, -deltaY, self.view.bounds.size.width, self.view.bounds.size.height);
        [self.view layoutIfNeeded];
    } completion:nil];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSUInteger curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
    
    [UIView animateWithDuration:duration delay:0.0 options:(UIViewAnimationOptions)curve animations:^{
        self.view.frame = self.view.bounds;
        [self.view layoutIfNeeded];
    } completion:nil];
}

- (void)keyboardDidHide:(NSNotification *)notification {
    self.mainView.textField = nil;
}


#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.mainView.textField = textField;
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (![textField isKindOfClass:[ZLAlertTextField class]]) {
        return NO;
    }
    
    ZLAlertTextField *zlTextField = (ZLAlertTextField *)textField;
    
    if (zlTextField.editChange != nil) {
        zlTextField.editChange(textField.text ?: @"");
    }
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (![textField isKindOfClass:[ZLAlertTextField class]]) {
        return;
    }
    
    ZLAlertTextField *zlTextField = (ZLAlertTextField *)textField;
    
    if (zlTextField.editEnd != nil) {
        zlTextField.editEnd(textField.text ?: @"");
    }
}


@end
